package com.aplikata.evok.ui;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;

import com.aplikata.bo.Node;
import com.aplikata.constants.ConstantsMsg;
import com.aplikata.dataModel.NodeDataModel;
import com.aplikata.service.NodeService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;


/** 
* @ClassName: EvokNodeBean 
* @Description: TODO
* @author Huyun
* @date Sep 28, 2012 1:20:21 PM 
*  
*/
@ManagedBean(name = "evokNodeBean")
@RequestScoped
public class EvokNodeBean extends UIBean {
	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{nodeService}")
	private NodeService nodeService;

	private List<Node> nodeList;
	private NodeDataModel nodeModel;

	private Node node;
	private Integer nodeType;

	public EvokNodeBean() {
		node = new Node();
	}

	public void resetNode() {
		node = new Node();
	}

	@SuppressWarnings("unchecked")
	public void queryNode() {
		node = new Node();
		if (FacesUtil.isKeyNull(nodeType)) {
			nodeList = null;
		} else {
			String hql = " from Node n where n.type = " + nodeType + " order by n.sort ";
			nodeList = (List<Node>) nodeService.find(hql);
		}
		if(nodeModel == null){
			nodeModel = new NodeDataModel(nodeService, nodeList);
		}else
			nodeModel.setWrappedData(nodeList);
	}

	public void submitNode() {
		if (node == null)
			return;

		node.setType(nodeType);
		try {
			nodeService.updateNodeForDynamicCategory(node);
			FacesUtil.addInfoMessage(ConstantsMsg.INFO_SUCCESS, new Object[] { node.getName() });
			queryNode();
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}

	public void deleteNode() {
		if (node == null || FacesUtil.isKeyNull(node.getId()))
			return;

		try {
			nodeService.deleteByKey(Node.class, node.getId());
			queryNode();
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}

	public void selectNodeRow(SelectEvent event) {
		node = (Node) event.getObject();
	}

	public void clearSelection() {
		RequestContext context = RequestContext.getCurrentInstance();
		context.execute("wdgList.clearSelection()");
	}

	public Node getNode() {
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
	}

	public List<Node> getNodeList() {
		return nodeList;
	}

	public void setNodeList(List<Node> nodeList) {
		this.nodeList = nodeList;
	}

	public Integer getNodeType() {
		return nodeType;
	}

	public void setNodeType(Integer nodeType) {
		this.nodeType = nodeType;
	}

	public NodeDataModel getNodeModel() {
		if (nodeModel == null) {
			nodeModel = new NodeDataModel(nodeService, getNodeList());
		}
		return nodeModel;
	}

	public void setNodeModel(NodeDataModel nodeModel) {
		this.nodeModel = nodeModel;
	}

	public NodeService getNodeService() {
		return nodeService;
	}

	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}

}
